<template>
  <div class="gi_table_page">
    <GiTable
      title="店铺信息管理"
      row-key="id"
      :data="dataList"
      :columns="columns"
      :loading="loading"
      :scroll="{ x: '100%', y: '100%', minWidth: 1000 }"
      :pagination="pagination"
      :disabled-tools="['size']"
      :disabled-column-keys="['name']"
      @refresh="search"
    >
      <template #toolbar-left>
        <GiForm v-model="queryForm" search :columns="queryFormColumns" size="medium" @search="search" @reset="reset"></GiForm>
      </template>
      <template #enable="{ record }">
        <span>
          <a-switch v-model="record.enable" @change="changeEnable(record)">
            <template #checked>
              启用
            </template>
            <template #unchecked>
              停用
            </template>
          </a-switch>
        </span>
      </template>
      <template #action="{ record }">
        <a-space>
          <a-link v-permission="['cakerun:shopInfo:update']" title="修改" @click="onUpdate(record)">修改</a-link>
        </a-space>
      </template>
      <template #toolbar-right>
        <a-button v-permission="['cakerun:shopInfo:export']" @click="onExport">
          <template #icon><icon-download /></template>
          <template #default>导出</template>
        </a-button>
      </template>
    </GiTable>
    <ShopInfoAddModal ref="ShopInfoAddModalRef" @save-success="search" />
  </div>
</template>

<script setup lang="ts">
import type { TableInstance } from '@arco-design/web-vue'
import ShopInfoAddModal from './ShopInfoAddModal.vue'
import {
  type ShopInfoQuery,
  exportShopInfo,
  listShopInfo,
  updateShopInfo,
} from '@/apis/cakerun/shopInfo'
import { useDownload, useTable } from '@/hooks'
import type { ColumnItem } from '@/components/GiForm'
import { type BrandAccountResp, listAllBrand } from '@/apis/cakerun/brandAccount'
import { isMobile } from '@/utils'
import has from '@/utils/has'

defineOptions({ name: 'ShopInfo' })

const brandInfo = ref<any[]>([])

const getBrandInfos = () => {
  listAllBrand({ enable: true }).then((res) => {
    const items: any = []
    res.data.forEach((item) => {
      items.push({ label: item.brand, value: item.id })
    })
    brandInfo.value = items
  })
}
const queryForm = reactive<ShopInfoQuery>({
  sort: ['id,desc'],
})
getBrandInfos()
const queryFormColumns: ColumnItem[] = reactive([
  {
    type: 'input',
    label: '门店名称',
    field: 'poiName',
    span: { xs: 24, sm: 6, xxl: 6 },
  },
  {
    type: 'select',
    label: '品牌',
    field: 'accountId',
    span: { xs: 24, sm: 6, xxl: 6 },
    props: {
      options: brandInfo,
      placeholder: '请选择品牌',
      multiple: true,
    },
  },
])
const {
  tableData: dataList,
  loading,
  pagination,
  search,
} = useTable((page) => listShopInfo({ ...queryForm, ...page }), { immediate: true })
const columns: TableInstance['columns'] = [
  { title: '品牌', dataIndex: 'accountName', slotName: 'accountName', width: 120 },
  { title: '门店名称', dataIndex: 'poiName', slotName: 'poiName', width: 200 },
  { title: '城市名称', dataIndex: 'cityName', slotName: 'cityName', width: 120 },
  { title: '店铺地址', dataIndex: 'address', slotName: 'address', width: 160 },
  { title: '营业部', dataIndex: 'businessDept', slotName: 'businessDept', width: 100 },
  { title: '片区', dataIndex: 'district', slotName: 'district', width: 100 },
  { title: '店号', dataIndex: 'shopNumber', slotName: 'shopNumber', width: 100 },
  { title: '是否启用', dataIndex: 'enable', slotName: 'enable', width: 90 },
  {
    title: '操作',
    dataIndex: 'action',
    slotName: 'action',
    width: 90,
    align: 'center',
    fixed: !isMobile() ? 'right' : undefined,
    show: has.hasPermOr(['cakerun:brandAccount:get', 'cakerun:brandAccount:update', 'cakerun:brandAccount:delete']),
  },
]

const changeEnable = (data) => {
  updateShopInfo(data, data.id)
}

// 重置
const reset = () => {
  search()
}

const ShopInfoAddModalRef = ref<InstanceType<typeof ShopInfoAddModal>>()
const onUpdate = (record: BrandAccountResp) => {
  ShopInfoAddModalRef.value?.onUpdate(record.id)
}
// 导出
const onExport = () => {
  useDownload(() => exportShopInfo(queryForm))
}
</script>

<style scoped lang="scss"></style>
